package com.yijushequ.action.product;


import com.yijushequ.action.BasicAction;
import com.yijushequ.dao.BaseUserDao;
import com.yijushequ.dao.ProductDao;
import com.yijushequ.domain.BaseUser;
import com.yijushequ.domain.Product;
import com.yijushequ.domain.ProductDetail;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace(BasicAction.BASE_NAME_SPACE)
public class ProductAction extends BasicAction {

    private Product product;
    private ProductDetail productDetail;
    private List<ProductDetail> productDetails;
    private String message;

    private ProductDao productDao;
    private BaseUserDao baseUserDao;

    @Action(value = "iniProC",
            results = {@Result(name = SUCCESS, type = "tiles", location = ".createProduct")}
    )
    public String iniProC() throws Exception {
        return SUCCESS;
    }


    @Action(value = "proC",
            results = {@Result(name = SUCCESS, type = "tiles", location = ".createProduct")}
    )
    public String proC() throws Exception {
        Product exist = productDao.findByName(product.getName());
        if (exist != null) this.product = exist;
        else productDao.persist(product);

        if (this.getSessionUserId() != null) {
            BaseUser baseUser = baseUserDao.findById(this.getSessionUserId());
            productDetail.setCreator(baseUser);
        }
        productDetail.setProduct(this.product);
        super.getModelDao().persist(productDetail);
        this.setProduct(null);
        this.setProductDetail(null);


        return SUCCESS;
    }

    @Action(value = "proL",
            results = {@Result(name = SUCCESS, type = "tiles", location = ".listProduct")}
    )
    public String proL() throws Exception {
        String name = "%";
        if (this.getProduct() != null && this.getProduct().getName() != null)
            name = "%" + this.getProduct().getName() + "%";
        this.productDetails = super.getModelDao().find("From ProductDetail p where p.product.name like ? group by p.id order by p.id desc ", this.getPagination(), name);
//        this.productDetails = super.getModelDao().find("From ProductDetail p where p.product.name like ? order by p.id desc",  name);
        return SUCCESS;
    }


    public List<ProductDetail> getProductDetails() {
        return productDetails;
    }

    public void setProductDetails(List<ProductDetail> productDetails) {
        this.productDetails = productDetails;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public ProductDetail getProductDetail() {
        return productDetail;
    }

    public void setProductDetail(ProductDetail productDetail) {
        this.productDetail = productDetail;
    }

    public ProductDao getProductDao() {
        return productDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public BaseUserDao getBaseUserDao() {
        return baseUserDao;
    }

    public void setBaseUserDao(BaseUserDao baseUserDao) {
        this.baseUserDao = baseUserDao;
    }
}
